<template>
  <!-- 授权用户 -->
  <el-dialog title="选择用户" v-model="visible" width="1000px" top="5vh" append-to-body>
    <el-row :gutter="20">
      <splitpanes :horizontal="false" class="default-theme">
        <!--部门数据-->
        <pane size="20">
          <el-col>
            <div class="head-container">
              <el-input v-model="deptName" placeholder="部门名称" clearable prefix-icon="Search"
                        style="margin-bottom: 20px"/>
            </div>
            <div class="head-container" style="height:340px;overflow-y: auto">
              <el-tree :data="deptOptions" :props="{ label: 'label', children: 'children' }"
                       :expand-on-click-node="false" :filter-node-method="filterNode" ref="deptTreeRef" node-key="id"
                       highlight-current default-expand-all @node-click="handleNodeClick"/>
            </div>
          </el-col>
        </pane>
        <pane size="80">
          <el-col>
            <el-form :model="queryParams" ref="queryRef" :inline="true">
              <el-form-item label="用户" prop="userName">
                <el-input
                    v-model="queryParams.userName"
                    placeholder="请输入账号或姓名"
                    clearable
                    style="width: 180px"
                    @keyup.enter="handleQuery"
                />
              </el-form-item>
              <el-form-item label="手机号码" prop="phonenumber">
                <el-input
                    v-model="queryParams.phonenumber"
                    placeholder="请输入手机号码"
                    clearable
                    style="width: 180px"
                    @keyup.enter="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <el-row>
              <el-table @row-click="clickRow" ref="refTable" :data="userList" @selection-change="handleSelectionChange"
                        height="300px">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="账号" prop="userName" :show-overflow-tooltip="true"/>
                <el-table-column label="姓名" prop="nickName" :show-overflow-tooltip="true"/>
                <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true"/>
                <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true"/>
                <el-table-column label="状态" align="left" prop="status">
                  <template #default="scope">
                    <dict-tag :options="sys_normal_disable" :value="scope.row.status"/>
                  </template>
                </el-table-column>
                <el-table-column label="创建时间" align="left" prop="createTime" width="180">
                  <template #default="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                  v-show="total > 0"
                  :total="total"
                  v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize"
                  @pagination="getList"
              />
            </el-row>
          </el-col>
        </pane>
      </splitpanes>
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSelectUser">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="SelectUser">
import {deptTreeSelectAll,deptTreeSelect, getSelectUserListAll,getSelectUserList} from "@/api/system/user";
import {Pane, Splitpanes} from "splitpanes";
import "splitpanes/dist/splitpanes.css"

const props = defineProps({
  selectNum: {
    type: [Number, String],
    default:1
  },
  auth:{
    type:[Boolean,String],
    default:false
  }
});

const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");

const userList = ref([]);
const visible = ref(false);
const total = ref(0);
const users = ref([]);
const deptOptions = ref(undefined);
const enabledDeptOptions = ref(undefined);
const deptName = ref("");

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  deptId: undefined,
  userName: undefined,
  phonenumber: undefined
});

// 显示弹框
function show() {
  getList();
  getDeptTree();
  visible.value = true;
}

/**选择行 */
function clickRow(row) {
  proxy.$refs["refTable"].toggleRowSelection(row);
}

// 多选框选中数据
function handleSelectionChange(selection) {
  users.value = selection;
}

// 查询表数据
function getList() {
  if(props.auth){
    getSelectUserList(queryParams).then(res => {
      userList.value = res.rows;
      total.value = res.total;
    });
  }else{
    getSelectUserListAll(queryParams).then(res => {
      userList.value = res.rows;
      total.value = res.total;
    });
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

const emit = defineEmits(["ok"]);

/** 选择授权用户操作 */
function handleSelectUser() {
  const uIds = users.value.join(",");
  if (uIds == "") {
    proxy.$modal.msgError("请选择用户");
    return;
  }
  if (props.selectNum > 0 && users.value.length > props.selectNum) {
    proxy.$modal.msgError("请选择" + props.selectNum + "个用户");
    return;
  }
  visible.value = false;
  emit("ok",users);
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  if(props.auth) {
    deptTreeSelect().then(response => {
      deptOptions.value = response.data;
      enabledDeptOptions.value = filterDisabledDept(JSON.parse(JSON.stringify(response.data)));
    });
  }else{
    deptTreeSelectAll().then(response => {
      deptOptions.value = response.data;
      enabledDeptOptions.value = filterDisabledDept(JSON.parse(JSON.stringify(response.data)));
    });
  }
}

/** 过滤禁用的部门 */
function filterDisabledDept(deptList) {
  return deptList.filter(dept => {
    if (dept.disabled) {
      return false;
    }
    if (dept.children && dept.children.length) {
      dept.children = filterDisabledDept(dept.children);
    }
    return true;
  });
}

/** 节点单击事件 */
function handleNodeClick(data) {
  queryParams.deptId = data.id;
  handleQuery();
}

/** 根据名称筛选部门树 */
watch(deptName, val => {
  proxy.$refs["deptTreeRef"].filter(val);
});

/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  if (!value) return true;
  return data.label.indexOf(value) !== -1;
};

defineExpose({
  show,
});
</script>
<style scoped>
.pagination-container {
  height:35px!important;
}
</style>
